package com.sgb.kjwl.viewmodel.home;

import android.app.Application;
import android.util.Log;


import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.manage.CategoriesGoodsEntity;
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity;
import com.sgb.kjwl.model.entity.user.ConcreteEntity;
import com.sgb.kjwl.model.entity.user.GoodsListEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author qianghe
 * 说明：经营业务-商品经营
 * 时间：2021.05.25
 */
public class WebSiteGoodsListModel extends AndroidViewModel {
    public MutableLiveData<WebsiteInfoEntity> websiteInfoEntityMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CategoriesGoodsEntity>> categoriesGoodsEntity = new MutableLiveData<>();
    public MutableLiveData<ListResponse<List<GoodsListEntity>>> goodsListEntity = new MutableLiveData<>();
    public MutableLiveData<ListResponse<List<ConcreteEntity>>> concreteListEntity = new MutableLiveData<>();
    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public WebSiteGoodsListModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getWebsiteInfo(String siteNo, int siteType) {
        Map<String, String> map = new HashMap<>(2);
        map.put("siteNo", siteNo);
        map.put("siteType", String.valueOf(siteType));
        APIManager.getInstance().getWebsiteInfoAPI().getSiteInfo(map).enqueue(new ICallBack<BaseEntity<WebsiteInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WebsiteInfoEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    websiteInfoEntityMutableLiveData.postValue(data.getData());
                }
            }
        });
    }

    public void getReverseLookupTreeNode(String companyNo) {
        APIManager.getInstance().getGoodsAPI().getReverseLookupTreeNode(companyNo).enqueue(new ICallBack<BaseEntity<List<CategoriesGoodsEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CategoriesGoodsEntity>> data) {
                String s =new Gson().toJson(data);
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    categoriesGoodsEntity.postValue(data.getData());
                }
            }
        });
    }

    public void getGoodsList(Map<String, Object> map, String areaCode) {
        map.put("areaCode", areaCode);
        NewAPIManager.getInstance().getGoodsAPI().getGoodsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<GoodsListEntity>>>>() {
            @Override
            public void onResponse(@NotNull Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, @NotNull Response<BaseEntity<ListResponse<List<GoodsListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    goodsListEntity.postValue(response.body().getData());
                }else {
                    msgLiveData.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(@NotNull Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, @NotNull Throwable t) {
            }
        });
    }

    public void getConcreteList(Map<String, Object> map, String areaCode) {
        map.put("areaCode", areaCode);
        NewAPIManager.getInstance().getGoodsAPI().getConcreteList(map).enqueue(new Callback<BaseEntity<ListResponse<List<ConcreteEntity>>>>() {
            @Override
            public void onResponse(@NotNull Call<BaseEntity<ListResponse<List<ConcreteEntity>>>> call, @NotNull Response<BaseEntity<ListResponse<List<ConcreteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    concreteListEntity.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(@NotNull Call<BaseEntity<ListResponse<List<ConcreteEntity>>>> call, @NotNull Throwable t) {

            }
        });
    }

}
